package com.example.juyuanchanting;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class shoujizhuce extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button back;
    private EditText eTtelephont;
    private EditText eTpassword;
    private EditText eTcfpassword;
    private Button bUtijiao;
    private String tel;
    private String pass;
    private String cfpass;
    private String tj;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shoujizhuce);
        initView();
        initDate();
        initEvent();
    }

    private void initDate(){
        tel = eTtelephont.getText().toString().trim();
        pass = eTpassword.getText().toString().trim();
        cfpass = eTcfpassword.getText().toString().trim();
        tj = bUtijiao.getText().toString().trim();
      String url = "http://anli-wechat.boruiman.com/foodserver/user/regist";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("tel","telephontEt");
            jsonObject.put("pass","passwordEt");
            jsonObject.put("cfpass","cfpasswordEt");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(shoujizhuce.this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e(TAG,"onResponse:"+jsonObject.optString("msg"));
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(shoujizhuce.this,"网络请求失败",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }
    private void initEvent() {
        bUtijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String telephont = eTtelephont.getText().toString().trim();
                String password = eTpassword.getText().toString().trim();
                String cfpassword = eTcfpassword.getText().toString().trim();
                Log.e(TAG, "onClick: "+ telephont);
                if (TextUtils.isEmpty(cfpassword)){
                    Toast.makeText(shoujizhuce.this,"重复密码不能为空", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(password)){
                    Toast.makeText(shoujizhuce.this, "密码不能为空", Toast.LENGTH_SHORT).show();

                }else if (TextUtils.isEmpty(telephont)){
                    Toast.makeText(shoujizhuce.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                }else if (password.equals(cfpassword)){
                    Toast.makeText(shoujizhuce.this, "注册成功"+telephont, Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(shoujizhuce.this,denglu.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(shoujizhuce.this, "密码不相等", Toast.LENGTH_SHORT).show();
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(shoujizhuce.this,denglu.class);
                startActivity(intent);
            }
        });
    }
    private void initView() {
        back = findViewById(R.id.btn_back);
        eTtelephont = findViewById(R.id.et_telephone);
        eTpassword = findViewById(R.id.et_password);
        eTcfpassword = findViewById(R.id.et_cfpassword);
        bUtijiao = findViewById(R.id.btn_tijiao);

    }
}

